package com.spring.config;

import java.io.InputStream;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

//import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**
 * Created by ZhaoTengchao on 2019/4/12.
 */
@Configuration
public class RestTemplateConfig {

//    @Autowired
//    private FastJsonHttpMessageConverter httpMessageConverter;

	@Bean
	RestTemplate restTemplate() throws Exception {
		HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
		factory.setConnectionRequestTimeout(5 * 60 * 1000);
		factory.setConnectTimeout(5 * 60 * 1000);
		factory.setReadTimeout(5 * 60 * 1000);
		// https
		SSLContextBuilder builder = new SSLContextBuilder();
		KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
		ClassPathResource resource = new ClassPathResource("xx.keystore");
		InputStream inputStream = resource.getInputStream();
		keyStore.load(inputStream, null);
		SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(builder.build(),
				NoopHostnameVerifier.INSTANCE);
		Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
				.register("http", new PlainConnectionSocketFactory()).register("https", socketFactory).build();
		PoolingHttpClientConnectionManager phccm = new PoolingHttpClientConnectionManager(registry);
		phccm.setMaxTotal(200);
		CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory)
				.setConnectionManager(phccm).setConnectionManagerShared(true).build();
		factory.setHttpClient(httpClient);
		RestTemplate restTemplate = new RestTemplate(factory);
		List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
		ArrayList<HttpMessageConverter<?>> convertersValid = new ArrayList<>();
		for (HttpMessageConverter<?> converter : converters) {
			if (converter instanceof MappingJackson2HttpMessageConverter
					|| converter instanceof MappingJackson2XmlHttpMessageConverter) {
				continue;
			}
			convertersValid.add(converter);
		}
//            convertersValid.add(httpMessageConverter);
		restTemplate.setMessageConverters(convertersValid);
		inputStream.close();
		return restTemplate;
	}
	
	public static void main(String[] args) {}
}